package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Capacity.Capactity;
import com.neu.cloudfactory.bean.Order.Ordering;
import com.neu.cloudfactory.bean.Product.Product;
import com.neu.cloudfactory.bean.Product.Producttype;
import com.neu.cloudfactory.repository.CapactityRepository;
import com.neu.cloudfactory.repository.OrderRepository;
import com.neu.cloudfactory.repository.ProductRepository;
import com.neu.cloudfactory.repository.ProducttypeRepository;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.NoSuchElementException;

@RestController
public class ProductControler {
    @Autowired
    ProductRepository productRepository;
    @Autowired
    ProducttypeRepository producttypeRepository;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    CapactityRepository capactityRepository;


    @RequestMapping("/product/add")
    public String addProduct(@RequestBody Product product){
        return productRepository.saveAndFlush(product).getId()==null?"failed":"success";
    }

    @RequestMapping("/product/findAll/{page}/{size}")
    public Page<Product> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return productRepository.findAll(pageable);
    }

    @RequestMapping("/product/deleteById/{id}")
    public String delete(@PathVariable("id") Integer id){
        try{
            Product product = productRepository.findById(id).get();
            productRepository.delete(product);
            orderRepository.saveAll(changeProductidOfOrder(id));
            capactityRepository.deleteAllByProductid(id);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    private List<Ordering> changeProductidOfOrder(int productid){
        List<Ordering> orders = orderRepository.findAllByProductid(productid);
        for(int i=0;i<orders.size();i++){
            orders.get(i).setProductid(1);
        }
        return orders;
    }

    @RequestMapping("/product/findById/{id}")
    public Product findById(@PathVariable("id") Integer id){
        return productRepository.findById(id).get();
    }

    @RequestMapping("/product/update")
    public String update(@RequestBody Product product){
        if(product.getId()!=null){
            productRepository.save(product);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/product/checkproduct/{id}")
    public String checkproduct(@PathVariable("id") Integer id){
        String result="";
        if(capactityRepository.existsByProductid(id)){
            result+="产能配置 ";
        }
        if (orderRepository.existsByProductid(id)){
            result+="商品订单 ";
        }
        return result;
    }

    @RequestMapping("/product/search/{page}/{size}/{input}")
    public Page<Product> search(@PathVariable("input") String input,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return productRepository.findAllByIdOrNameContainingOrTypeidOrSizeContainingOrDescriptionContaining(id,input,id,input,input,pageable);
    }

    @RequestMapping("/producttype/findAll/{page}/{size}")
    public Page<Producttype> typefindAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return producttypeRepository.findAll(pageable);
    }

    @RequestMapping("/producttyoe/add")
    public String addType(@RequestBody Producttype producttype){
        return producttypeRepository.saveAndFlush(producttype).getId()==null?"failed":"success";
    }

    @RequestMapping("/producttyoe/deleteById/{id}")
    public String deletetype(@PathVariable("id") Integer id){
        try{
            Producttype producttype = producttypeRepository.findById(id).get();
            producttypeRepository.delete(producttype);
            List<Product> products = productRepository.findAllByTypeid(id);
            for(int i=0;i<products.size();i++){
                products.get(i).setTypeid(1);
            }
            productRepository.saveAll(products);


            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    @RequestMapping("/producttyoe/findById/{id}")
    public Producttype findtypeById(@PathVariable("id") Integer id){
        return producttypeRepository.findById(id).get();
    }

    @RequestMapping("/producttyoe/update")
    public String update(@RequestBody Producttype producttype){
        if(producttype.getId()!=null){
            producttypeRepository.save(producttype);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/producttyoe/search/{page}/{size}/{input}")
    public Page<Producttype> typesearch(@PathVariable("input") String input, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return producttypeRepository.findAllByIdOrTypenameContainingOrDescriptionContaining(id,input,input,pageable);
    }

    @RequestMapping("/producttyoe/checkproducttyoe/{typeid}")
    public String checkproducttyoe(@PathVariable("typeid") Integer typeid){
        return productRepository.existsByTypeid(typeid)?"used":"";
    }
}
